package com.video.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.video.common.dto.LoginDto;
import com.video.common.dto.ResetDto;
import com.video.common.lang.Result;
import com.video.entity.User;
import com.video.service.UserService;
import com.video.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author maomao
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        User user = userService.getById(id);

        Assert.notNull(user, "用户不存在");

        return Result.success(200, "操作成功", user);
    }

    @RequiresAuthentication
    @PostMapping("/save")
    public Result save(@Validated @RequestBody User user) {
        userService.updateById(user);
        return Result.success(200, "操作成功", null);
    }

    @RequiresAuthentication
    @PostMapping("/reset/password")
    public Result save(@Validated @RequestBody ResetDto resetDto){

        User temp;

        temp = userService.getById(resetDto.getId());

        Assert.isTrue(temp.getId().longValue() == ShiroUtils.getProfile().getId().longValue(), "没有权限修改");

        temp.setPassword(SecureUtil.md5(resetDto.getPassword()));

        userService.updateById(temp);

        return Result.success(200, "操作成功", null);
    }
}
